﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BaiduFanyi
{
    public partial class TranslateForm : Form
    {
        public TranslateForm()
        {
            InitializeComponent();
        }

        private void btnTran_Click(object sender, EventArgs e)
        {
            StringBuilder sbValue = new StringBuilder();
            StringBuilder sbErroe = new StringBuilder();
            //key 原文 Value译文集合
            Dictionary<string, List<string>> fromValues = new Dictionary<string, List<string>>();
            foreach (var line in txtFrom.Lines)
            {
                if (!fromValues.ContainsKey(line) && string.IsNullOrWhiteSpace(line) == false)
                {
                    fromValues.Add(line, new List<string>());
                    sbValue.AppendLine(line);
                }
            }

            List<Task> lisTasks = new List<Task>();
            foreach (string toLanguage in Launge.ToValues.Values)
            {
                TranslateResult translateResult = TranslateHelp.Translate(sbValue.ToString(), Launge.简体中文, toLanguage);
                foreach (Trans_Result transResult in translateResult.trans_result)
                {
                    if (!fromValues.TryGetValue(transResult.src, out List<string> lisValues))
                    {
                        continue;
                    }
                    lisValues.Add(transResult.dst);
                }
            }

            StringBuilder sbLanguage = new StringBuilder();
            ResourceHelp resourceHelp = new ResourceHelp();
           
            foreach (KeyValuePair<string, List<string>> keyValuePair in fromValues)
            {
                if (keyValuePair.Value.Count!= Launge.Values.Count)
                {
                    sbErroe.AppendLine($"异常的翻译结果  {keyValuePair.Key}");
                    continue;
                }

                int i = 0;

                //这里ID可能会变化
                resourceHelp.AddValue("ID", keyValuePair.Key);
                resourceHelp.AddValue("zh-cn", keyValuePair.Key);
                foreach (string laungeName in Launge.Values.Values)
                {
                    resourceHelp.AddValue(laungeName, keyValuePair.Value[i]);
                    i++;
                }

                sbLanguage.Append(resourceHelp.ToString());
            }

            txtLanguage.Text = sbLanguage.ToString();





        }

        private void btn测试AddResource_Click(object sender, EventArgs e)
        {
            ResourceHelp resourceHelp = new ResourceHelp();
            resourceHelp.AddValue("ID", "类型");
            resourceHelp.AddValue("zh-cn", "类型");
            resourceHelp.AddValue("vi-vn", "Các loại");
            txtLanguage.Text = resourceHelp.ToString();
           
        }

        private void btn多线程翻译_Click(object sender, EventArgs e)
        {
            StringBuilder sbValue = new StringBuilder();
            StringBuilder sbErroe = new StringBuilder();
            //key 原文 Value译文集合
            Dictionary<string, List<string>> fromValues = new Dictionary<string, List<string>>();
            ConcurrentBag < TranslateResult > lisResults=new ConcurrentBag<TranslateResult>();
            foreach (var line in txtFrom.Lines)
            {
                if (!fromValues.ContainsKey(line) && string.IsNullOrWhiteSpace(line) == false)
                {
                    fromValues.Add(line, new List<string>());
                    sbValue.AppendLine(line);
                }
            }

            List<Task> lisTasks = new List<Task>();
            foreach (string toLanguage in Launge.ToValues.Values)
            {
                Task task=new Task(() =>
                {
                    TranslateResult translateResult = TranslateHelp.Translate(sbValue.ToString(), Launge.简体中文, toLanguage);
                    lisResults.Add(translateResult);
                });
                lisTasks.Add(task);
                task.Start();
            }

            Task.WaitAll(lisTasks.ToArray());


            //进行排序(按照简体中文)
            TranslateResult[] TranslateResults=new TranslateResult[lisResults.Count];
           
            foreach (TranslateResult translateResult in lisResults)
            {
                if (!Launge.BaiduIndex.TryGetValue(translateResult.to,out int index))
                {
                    sbErroe.AppendLine($"语言  {translateResult.to}  没有查询到");
                    continue;
                }

                TranslateResults[index] = translateResult;
            }

            //将结果写入
            foreach (TranslateResult translateResult in TranslateResults)
            {
                foreach (Trans_Result transResult in translateResult.trans_result)
                {
                    if (!fromValues.TryGetValue(transResult.src, out List<string> lisValues))
                    {
                        sbErroe.AppendLine($"没有结果的翻译{transResult.src}   语种{translateResult.to}");
                        continue;
                    }
                    lisValues.Add(transResult.dst);
                }
            }

           



            StringBuilder sbLanguage = new StringBuilder();
            StringBuilder sbApp = new StringBuilder();
          

            foreach (KeyValuePair<string, List<string>> keyValuePair in fromValues)
            {
                if (keyValuePair.Value.Count != Launge.Values.Count)
                {
                    sbErroe.AppendLine($"异常的翻译结果  {keyValuePair.Key}");
                    continue;
                }

                int i = 0;
                ResourceHelp resourceHelp = new ResourceHelp();
                //这里ID可能会变化
                string idValue = GetID(keyValuePair.Key);
                resourceHelp.AddValue("ID", idValue);
                sbApp.AppendLine($"public static string {idValue} => LanHelper.GetLanguage();");
                resourceHelp.AddValue("zh-cn", keyValuePair.Key);
                foreach (string laungeName in Launge.Values.Values)
                {
                    resourceHelp.AddValue(laungeName, keyValuePair.Value[i]);
                    i++;
                }

                sbLanguage.Append(resourceHelp.ToString());
            }
            txtApp.Clear();
            txtApp.Text = sbApp.ToString();
            txtLanguage.Clear();
            txtLanguage.Text = sbLanguage.ToString();
        }

        /// <summary>
        /// 替换掉括号
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static string GetID(string id)
        {
            string result = id.Replace("{0}", "_");
             result = result.Replace("{1}", "_");
             result = result.Replace("{2}", "_");
             result = result.Replace("{3}", "_");
             result = result.Replace("{4}", "_");
             result = result.Replace("{5}", "_");
             result = result.Replace("{6}", "_");
             result = result.Replace("{7}", "_");
             result = result.Replace("{8}", "_");
             result = result.Replace("{9}", "_");
             result = result.Replace("{10}", "_");
            return result;
        }
    }
}
